import comTitle from "@/views/com/com_title.vue";
import buyingIfor from "./buyingIfor/buyingIfor.vue";
import choiceShop from "./choiceShop.vue";
import cropperModal from "@/components/cropper/index.vue";
import { fileBase64, toDecimal2 } from '@/utils/util'
import { shopInstockGoodsNewAdd, rejectedreason } from '@/api/shop/stock/instock.js'
import { upLoad } from '@/api/shop/upLoad.js'
import config from '@/config/appConfig'

export default {
    components: {
        comTitle,
        buyingIfor,
        choiceShop,
        cropperModal
    },
    data() {
        return {
            labelWidth: "120px",
            disabled: false,
            ruleForm: {
            },
            rules: {
                inStockReason: [
                    { required: true, message: "请选择入库原因", trigger: 'change' },
                ]
            },
            mendianList: [  //门店list

            ],

            imageUrl: "",
            loading: false,  //上传
            optionsData: {
                title: "商品图片",     //弹框表头
                img: '',   //裁切图片地址
                autoCrop: true, //是否默认生成截图框
                autoCropWidth: 200, //默认生成截图框宽度
                autoCropHeight: 200, //默认生成截图框高度
                fixedBox: true //固定截图框大小 不允许改变
            },
            number: 0,
            fileList: [],
            fileListUrl: [], //放大展示
            imgUrlList: [],
            inStockReasonList: []  //原因
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            rejectedreason('01').then(val => {
                let data = val.data
                this.inStockReasonList = data.map(item => {
                    return {
                        label: item.inOutReason,
                        value: item.inOutReasonId
                    }
                })
            })
        },
        submitSave() {   //提交并保存
            let obj = { ...this.$refs['buyingIfor'].dataList[0] }

            let urlList = [...this.imgUrlList]
            if (urlList.length) {  //图片处理
                let imgList = []
                urlList.forEach(item => {
                    if (item.url.indexOf(config.url) > -1) {  //截取路径
                        item.url = item.url.replace(config.url, '')
                    }
                    imgList.push(item.url)
                })
                this.ruleForm.inStockCert = imgList.join(',')
            }

            console.log(Object.assign({}, this.ruleForm, obj))
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    if (this.ruleForm.inStockTime) {
                        if (obj.shopInStockNewAddGoodsDetail.length) {
                            shopInstockGoodsNewAdd(Object.assign({ deptId: this.$store.getters.userInfo.sysUser.deptId, tenantId: this.$store.getters.userInfo.sysUser.tenantId }, this.ruleForm, obj)).then(() => {
                                this.quxiao('ruleForm')
                            })
                        } else {
                            this.$message.warning('请至少选择一种商品')
                        }

                    } else {
                        this.$message.warning('请选择入库日期')
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        quxiao(ruleForm) {  //取消
            this.$router.go(-1)
            this.$refs[ruleForm].resetFields();
            this.ruleForm = {

            }
        },
        addStore() {
            this.$refs.choiceShop.dialogVisibleTile = '添加直送门店'
            this.$refs.choiceShop.dialogVisible = true
        },
        trigger(data) {
            this.$refs.buyingIfor.dataList = []
            data.chioseVal.forEach(i => {
                this.$refs.buyingIfor.dataList.push({   //将门店push 到采购信息中去
                    mendian: i
                })
            });
        },
        pslxChange(e) {           //总仓还是门店
            this.$refs.buyingIfor.dataList = []
            if (e.target.value == 1) {
                this.$refs.buyingIfor.dataList.push({ loadData: [{}] })
            } else {
                this.$refs.buyingIfor.dataList = []
            }
        },
        cxsjChange(e) {
            this.ruleForm.inStockTime = this.formDate(e._d)
            console.log(this.ruleForm)
        },
        handleChange(info) {  //上传触发
            let { options } = this;
            console.log(info)
            fileBase64(info.file.originFileObj).then(result => {
                let target = Object.assign({}, options, {
                    img: result.result,
                    files: result.files
                })
                this.$refs['cropperModal'].edit(target);
            })
        },
        beforeUpload(file) {
            const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
            if (!isJpgOrPng) {
                this.$message.error('图片格式不支持！');
            }
            const isLt1M = file.size / 1024 / 1024 < 1;
            if (!isLt1M) {
                this.$message.error('图片大小限制在1MB内!');
            }
            return isJpgOrPng && isLt1M;
        },
        setZhutu() {

        },
        delUplod(file) {
            const index = this.fileList.indexOf(file)
            const newFileList = this.fileList.slice()
            newFileList.splice(index, 1)
            this.fileList = newFileList
        },
        handleCropperSuccess(data) {  //图片裁切完成保存
            // fileBase64(data).then(result => {
            //     this.imageUrl = result.result
            //     this.fileListUrl.push(this.imageUrl)
            //     this.fileList.push({
            //         uid: this.number++,
            //         name: 'image.png',
            //         status: 'done',
            //         url: this.imageUrl
            //     })
            // })
            upLoad(data).then(res => {
                console.log(res)
                this.imgUrlList.push(res.data)
                this.fileList.push({
                    uid: this.number++,
                    name: 'image.png',
                    status: 'done',
                    url: URL.createObjectURL(data)
                })
                this.$message.success('上传成功！')
            }).catch(() => {
                this.$message.warning('上传失败，请重新上传！')
            })
        },
    },

} 